// SPDX-License-Identifier: GPL-2.0+

#include <common.h>
#include <malloc.h>
#include <clk.h>
#include <led.h>
#include <pwm.h>
#include <pci.h>
#include <net.h>
#include <power/regulator.h>

#include <asm/io.h>
#include <asm/gpio.h>

#include <mach/loongson.h>

#include "../common/platform.h"

DECLARE_GLOBAL_DATA_PTR;

static void ls2x_set_nand(int enable)
{
	u32 val = 0;

	/* 设置nand gpio复用 nand_sel=1 */
	val = readl((unsigned int *)PHYS_TO_UNCACHED(LS2X_MUX_BASE));
	if (enable) {
		val = val | (1<<9);
	} else {
		val = val & (~(1<<9));
	}
	writel(val, (unsigned int *)PHYS_TO_UNCACHED(LS2X_MUX_BASE));
}

#ifndef NAND_LS2K_DMACHAN
#define NAND_LS2K_DMACHAN 0
#endif
#ifndef SDIO_LS2K_DMACHAN
#define SDIO_LS2K_DMACHAN 4
#endif

/* 设置设备使用的apb dma通道 */
static void ls2x_set_apbdma(void)
{
	u32 val;

	val = (NAND_LS2K_DMACHAN<<0) |
		(1<<3) | (2<<6) | (1<<9) | (2<<12) | (SDIO_LS2K_DMACHAN<<15) | (1<<18) | (3<<21);
	__raw_writel(val, (unsigned int *)PHYS_TO_UNCACHED(LS2X_APB_DMA_CFG));
}

int board_early_init_f(void)
{
	ls2x_set_apbdma();
#ifdef CONFIG_NAND_LS1X
	ls2x_set_nand(1);
#else
	ls2x_set_nand(0);
#endif
	return 0;
}

int ls2k_board_late_init(void)
{
#ifdef BEEP_GPIO
	{
	int ret;
	ret = gpio_request(BEEP_GPIO, "beep");
	if (ret == 0) {
		int i;
		for (i=0; i<500; i++) {
			gpio_direction_output(BEEP_GPIO, i & 0x1);
			udelay(220);
		}
		gpio_direction_output(BEEP_GPIO, 0);
	}
	}
#endif
	return 0;
}
